﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.Practices.Unity;
using System.Web.SessionState;
using System.Collections;

namespace Liya.Crm.Core
{
    public class UnityControllerFactory : DefaultControllerFactory
    {
        IUnityContainer container;
        public UnityControllerFactory(IUnityContainer container)
        {
            this.container = container;
        }

        protected override IController GetControllerInstance(RequestContext reqContext,
            Type controllerType)
        {
            if (controllerType == null)
                return null;
            return container.Resolve(controllerType) as IController;

            //IController controllerInstance = null;
            //if (controllerType != null)
            //{
            //    IDictionary objectsOfType = ApplicationContext.GetObjectsOfType(controllerType);
            //    if (objectsOfType.Count > 0)
            //    {
            //        controllerInstance = (IController)objectsOfType.Cast<DictionaryEntry>().First<DictionaryEntry>().Value;
            //    }
            //}
            //else // there should be if (contollerInstance == null)
            //{
            //    controllerInstance = base.GetControllerInstance(reqContext, controllerType);
            //}
            //this.AddActionInvokerTo(controllerInstance);
            //return controllerInstance;
        }

    }

}